﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Com.LongER.Business
{
    public abstract class ABussinessBase
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="type"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        protected SqlParameter getSqlParameter(String key, SqlDbType type, Object val)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = key;
            param.SqlDbType = type;
            param.IsNullable = true;
            param.Value = ((val == null) || String.IsNullOrEmpty(val.ToString())) ? DBNull.Value : val;
            return param;
        }

        /// <summary>
        /// dummmy [{label: "博客园", value: "cnblogs"}, {label: "囧月", value: "囧月"}]
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        protected string DataTable2Json(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return "";
            }

            StringBuilder jsonBuilder = new StringBuilder();
            // jsonBuilder.Append("{"); 
            //jsonBuilder.Append(dt.TableName.ToString());  
            jsonBuilder.Append("[");//转换成多个model的形式
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            //  jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }

    }
}
